//
//  LZKButtonAddition.m
//  lzk
//
//  Created by mengchangMBP on 8/18/16.
//  Copyright © 2016 zjn. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <objc/runtime.h>

#import "LZKButtonAddition.h"
#import "LZKImageAddition.h"

static char *viewClickKey;

@implementation UIButton (LZKButtonAddition)

#pragma mark - title
- (void)setTitleForStateNormal:(NSString *)title {
    [self setTitle:title forState:UIControlStateNormal];
}

- (NSString *)titleForStateNormal {
    return [self titleForState:UIControlStateNormal];
}

- (void)setTitleForStateHighlighted:(NSString *)title {
    [self setTitle:title forState:UIControlStateHighlighted];
}

- (NSString *)titleForStateHighlighted {
    return [self titleForState:UIControlStateHighlighted];
}

- (void)setTitleForStateSelected:(NSString *)title {
    [self setTitle:title forState:UIControlStateSelected];
}

- (NSString *)titleForStateSelected {
    return [self titleForState:UIControlStateSelected];
}

- (void)setTitleForStateDisabled:(NSString *)title {
    [self setTitle:title forState:UIControlStateDisabled];
}

- (NSString *)titleForStateDisabled {
    return [self titleForState:UIControlStateDisabled];
}

- (void)setTitleForStateHighlightedWhenSelected:(NSString *)titleForStateHighlightedWhenSelected {
    [self setTitle:titleForStateHighlightedWhenSelected forState:UIControlStateSelected|UIControlStateHighlighted];
}

- (NSString *)titleForStateHighlightedWhenSelected {
    return [self titleForState:UIControlStateSelected|UIControlStateHighlighted];
}

- (void)setTitleForStateDisabledWhenSelected:(NSString *)titleForStateDisabledWhenSelected {
    [self setTitle:titleForStateDisabledWhenSelected forState:UIControlStateSelected|UIControlStateDisabled];
}

- (NSString *)titleForStateDisabledWhenSelected {
    return [self titleForState:UIControlStateSelected|UIControlStateDisabled];
}

#pragma mark - title color
- (void)setTitleColorForStateNormal:(UIColor *)color {
    [self setTitleColor:color forState:UIControlStateNormal];
}

- (UIColor *)titleColorForStateNormal {
    return [self titleColorForState:UIControlStateNormal];
}

- (void)setTitleColorForStateHighlighted:(UIColor *)color {
    [self setTitleColor:color forState:UIControlStateHighlighted];
}

- (UIColor *)titleColorForStateHighlighted {
    return [self titleColorForState:UIControlStateHighlighted];
}

- (void)setTitleColorForStateSelected:(UIColor *)color {
    [self setTitleColor:color forState:UIControlStateSelected];
}

- (UIColor *)titleColorForStateSelected {
    return [self titleColorForState:UIControlStateSelected];
}

- (void)setTitleColorForStateDisabled:(UIColor *)color {
    [self setTitleColor:color forState:UIControlStateDisabled];
}

- (UIColor *)titleColorForStateDisabled {
    return [self titleColorForState:UIControlStateDisabled];
}

- (void)setTitleColorForStateHighlightedWhenSelected:(UIColor *)titleColorForStateHighlightedWhenSelected {
    [self setTitleColor:titleColorForStateHighlightedWhenSelected forState:UIControlStateSelected|UIControlStateHighlighted];
}

- (UIColor *)titleColorForStateHighlightedWhenSelected {
    return [self titleColorForState:UIControlStateSelected|UIControlStateHighlighted];
}

- (void)setTitleColorForStateDisabledWhenSelected:(UIColor *)titleColorForStateDisabledWhenSelected {
    [self setTitleColor:titleColorForStateDisabledWhenSelected forState:UIControlStateSelected|UIControlStateDisabled];
}

- (UIColor *)titleColorForStateDisabledWhenSelected {
    return [self titleColorForState:UIControlStateDisabled|UIControlStateSelected];
}

#pragma mark - title shadow color
- (void)setTitleShadowColorForStateNormal:(UIColor *)color {
    [self setTitleShadowColor:color forState:UIControlStateNormal];
}

- (UIColor *)titleShadowColorForStateNormal {
    return [self titleShadowColorForState:UIControlStateNormal];
}

- (void)setTitleShadowColorForStateHighlighted:(UIColor *)color {
    [self setTitleShadowColor:color forState:UIControlStateHighlighted];
}

- (UIColor *)titleShadowColorForStateHighlighted {
    return [self titleShadowColorForState:UIControlStateHighlighted];
}

- (void)setTitleShadowColorForStateSelected:(UIColor *)color {
    [self setTitleShadowColor:color forState:UIControlStateSelected];
}

- (UIColor *)titleShadowColorForStateSelected {
    return [self titleShadowColorForState:UIControlStateSelected];
}

- (void)setTitleShadowColorForStateDisabled:(UIColor *)color {
    [self setTitleShadowColor:color forState:UIControlStateDisabled];
}

- (UIColor *)titleShadowColorForStateDisabled {
    return [self titleShadowColorForState:UIControlStateDisabled];
}

- (void)setTitleShadowColorForStateDisabledWhenSelected:(UIColor *)titleShadowColorForStateDisabledWhenSelected {
    [self setTitleShadowColor:titleShadowColorForStateDisabledWhenSelected forState:UIControlStateDisabled|UIControlStateSelected];
}

- (UIColor *)titleShadowColorForStateDisabledWhenSelected {
    return [self titleShadowColorForState:UIControlStateSelected|UIControlStateDisabled];
}

- (void)setTitleShadowColorForStateHighlightedWhenSelected:(UIColor *)titleShadowColorForStateHighlightedWhenSelected {
    [self setTitleShadowColor:titleShadowColorForStateHighlightedWhenSelected forState:UIControlStateSelected|UIControlStateHighlighted];
}

- (UIColor *)titleShadowColorForStateHighlightedWhenSelected {
    return [self titleShadowColorForState:UIControlStateHighlighted|UIControlStateSelected];
}

#pragma mark - image
- (void)setImageForStateNormal:(UIImage *)image {
    [self setImage:image forState:UIControlStateNormal];
}

- (UIImage *)imageForStateNormal {
    return [self imageForState:UIControlStateNormal];
}

- (void)setImageForStateHighlighted:(UIImage *)image {
    [self setImage:image forState:UIControlStateHighlighted];
}

- (UIImage *)imageForStateHighlighted {
    return [self imageForState:UIControlStateHighlighted];
}

- (void)setImageForStateSelected:(UIImage *)image {
    [self setImage:image forState:UIControlStateSelected];
}

- (UIImage *)imageForStateSelected {
    return [self imageForState:UIControlStateSelected];
}

- (void)setImageForStateDisabled:(UIImage *)image {
    [self setImage:image forState:UIControlStateDisabled];
}

- (UIImage *)imageForStateDisabled {
    return [self imageForState:UIControlStateDisabled];
}

- (void)setImageForStateDisabledWhenSelected:(UIImage *)imageForStateDisabledWhenSelected {
    [self setImage:imageForStateDisabledWhenSelected forState:UIControlStateSelected|UIControlStateDisabled];
}

- (UIImage *)imageForStateDisabledWhenSelected {
    return [self imageForState:UIControlStateDisabled|UIControlStateSelected];
}

- (void)setImageForStateHighlightedWhenSelected:(UIImage *)imageForStateHighlightedWhenSelected {
    [self setImage:imageForStateHighlightedWhenSelected forState:UIControlStateSelected|UIControlStateHighlighted];
}

- (UIImage *)imageForStateHighlightedWhenSelected {
    return [self imageForState:UIControlStateHighlighted|UIControlStateSelected];
}

#pragma mark - background image
- (void)setBackgroundImageForStateNormal:(UIImage *)image {
    [self setBackgroundImage:image forState:UIControlStateNormal];
}

- (UIImage *)backgroundImageForStateNormal {
    return [self backgroundImageForState:UIControlStateNormal];
}

- (void)setBackgroundImageForStateHighlighted:(UIImage *)image {
    [self setBackgroundImage:image forState:UIControlStateHighlighted];
}

- (UIImage *)backgroundImageForStateHighlighted {
    return [self backgroundImageForState:UIControlStateHighlighted];
}

- (void)setBackgroundImageForStateSelected:(UIImage *)image {
    [self setBackgroundImage:image forState:UIControlStateSelected];
}

- (UIImage *)backgroundImageForStateSelected {
    return [self backgroundImageForState:UIControlStateSelected];
}

- (void)setBackgroundImageForStateDisabled:(UIImage *)image {
    [self setBackgroundImage:image forState:UIControlStateDisabled];
}

- (UIImage *)backgroundImageForStateDisabled {
    return [self backgroundImageForState:UIControlStateDisabled];
}

- (void)setBackgroundImageForStateDisabledWhenSelected:(UIImage *)imageForStateDisabledWhenSelected {
    [self setBackgroundImage:imageForStateDisabledWhenSelected forState:UIControlStateSelected|UIControlStateDisabled];
}

- (UIImage *)backgroundImageForStateDisabledWhenSelected {
    return [self backgroundImageForState:UIControlStateDisabled|UIControlStateSelected];
}

- (void)setBackgroundImageForStateHighlightedWhenSelected:(UIImage *)imageForStateHighlightedWhenSelected {
    [self setBackgroundImage:imageForStateHighlightedWhenSelected forState:UIControlStateSelected|UIControlStateHighlighted];
}

- (UIImage *)backgroundImageForStateHighlightedWhenSelected {
    return [self backgroundImageForState:UIControlStateHighlighted|UIControlStateSelected];
}

#pragma mark - background color

- (void)setBackgroundColorForStateNormal:(UIColor *)bgcolor {
    [self setBackgroundColor:bgcolor forState:UIControlStateNormal];
}

- (void)setBackgroundColorForStateHighlighted:(UIColor *)bgcolor {
    [self setBackgroundColor:bgcolor forState:UIControlStateHighlighted];
}

- (void)setBackgroundColorForStateSelected:(UIColor *)bgcolor {
    [self setBackgroundColor:bgcolor forState:UIControlStateSelected];
}

- (void)setBackgroundColorForStateDisabled:(UIColor *)bgcolor {
    [self setBackgroundColor:bgcolor forState:UIControlStateDisabled];
}

- (void)setBackgroundColorForStateDisabledWhenSelected:(UIColor *)bgcolor {
    [self setBackgroundColor:bgcolor forState:UIControlStateDisabled|UIControlStateSelected];
}

- (void)setBackgroundColorForStateHighlightedWhenSelected:(UIColor *)bgcolor {
    [self setBackgroundColor:bgcolor forState:UIControlStateHighlighted|UIControlStateSelected];
}

- (UIColor *)backgroundColorForStateNormal {
    return [self backgroundColorForState:UIControlStateNormal];
}

- (UIColor *)backgroundColorForStateHighlighted {
    return [self backgroundColorForState:UIControlStateHighlighted];
}

- (UIColor *)backgroundColorForStateSelected {
    return [self backgroundColorForState:UIControlStateSelected];
}

- (UIColor *)backgroundColorForStateDisabled {
    return [self backgroundColorForState:UIControlStateDisabled];
}

- (UIColor *)backgroundColorForStateDisabledWhenSelected {
    return [self backgroundColorForState:UIControlStateSelected|UIControlStateDisabled];
}

- (UIColor *)backgroundColorForStateHighlightedWhenSelected {
    return [self backgroundColorForState:UIControlStateSelected|UIControlStateHighlighted];
}

- (NSMutableDictionary *)backgroundColorObjects {
    NSMutableDictionary *dictionary = objc_getAssociatedObject(self, @selector(backgroundColorForState:));
    if (dictionary) {
        return dictionary;
    }
    dictionary = [NSMutableDictionary dictionary];
    objc_setAssociatedObject(self, @selector(backgroundColorForState:), dictionary, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    return dictionary;
}

- (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state {
    UIImage             *image      = nil;
    NSMutableDictionary *dictionary = [self backgroundColorObjects];
    NSString            *key        = [NSString stringWithFormat:@"%@", [NSNumber numberWithUnsignedInteger:state]];
    if (backgroundColor) {
        [dictionary setObject:backgroundColor forKey:key];
        image = [UIImage imageWithColor:backgroundColor];
    } else {
        [dictionary removeObjectForKey:key];
    }
    [self setBackgroundImage:image forState:state];
}

- (UIColor *)backgroundColorForState:(UIControlState)state {
    NSString            *key        = [NSString stringWithFormat:@"%@", [NSNumber numberWithUnsignedInteger:state]];
    NSMutableDictionary *dictionary = [self backgroundColorObjects];
    return [dictionary objectForKey:key];
}
@end